package com.ssm.java.utiles;

import com.ssm.java.entity.Users;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author 隔壁_老陈
 * 代码风骚、效率恐怖、编译器深深鄙视智商。
 * 千里之外定位问题、瞬间修复依旧风骚……
 * 自定义拦截器
 * @create 2021-01-06 11:19
 */
public class MyInterHandler implements HandlerInterceptor {

    private List<String> excludeUrls;

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
       //获取对应的Session数据
        Users users=(Users)httpServletRequest.getSession().getAttribute("usersInfo");

        //获取请求的路径地址
        String requestURI = httpServletRequest.getRequestURI();

        for (String excludeUrl : excludeUrls) {
            if (requestURI.contains(excludeUrl)) {
                return true;
            }
        }
        if (users==null) {
            /*httpServletResponse.sendRedirect("/view/login.jsp");*/
        }

        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }

    public List<String> getExcludeUrls() {
        return excludeUrls;
    }

    public void setExcludeUrls(List<String> excludeUrls) {
        this.excludeUrls = excludeUrls;
    }
}
